using System;
using System.Collections.Generic;
using System.Text;
using RideMe.Presentation.WebServices;
using RideMe.Presentation.Properties;
using RideMe.Core;
using System.Security.Principal;

namespace RideMe.Presentation
{
    public class LogService : log_exceptionService
    {
        public bool LogException(DateTime exceptionTime, Exception e)
        {
            string basePath = AppDomain.CurrentDomain.BaseDirectory;
            if (!FileSystemService.IsFile(Resources.LogFile))
            {
                FileSystemService.SaveDocument(basePath + Resources.LogFile, this.BuildExceptionString(e, exceptionTime));
            }
            else
            {
                FileSystemService.AppendDocument(basePath + Resources.LogFile, this.BuildExceptionString(e, exceptionTime));
            }
            
            return this.LogException(exceptionTime, e.ToString());
        }

        /// <summary>
        /// Creates a string from the exception.
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        private string BuildExceptionString(Exception exception, DateTime timeOfException)
        {
            IIdentity user = WindowsIdentity.GetCurrent();
            string machineName = System.Environment.MachineName;

            string message = timeOfException.ToShortDateString() + " : " + timeOfException.ToShortTimeString()
                    + " : " + user.Name + " : " + machineName + " : " + exception.ToString() + " : "
                    + exception.Source + " : " + exception.Message;
            return message;
        }
    }
}
